package redora.configuration.rdo.service;

import java.util.List;
import java.util.Set;
import java.util.Vector;

import redora.api.BusinessRuleViolation;
import redora.api.fetch.*;
import redora.configuration.rdo.model.RedoraTrash;
import redora.exceptions.BusinessRuleViolationException;
import redora.exceptions.ConnectException;
import redora.exceptions.CopyException;
import redora.exceptions.PagingException;
import redora.exceptions.PersistException;
import redora.exceptions.QueryException;
import redora.configuration.rdo.service.ServiceFactory;

public class TrashService {
    /**
     * Create a new record in the RedoraTrash table. You still have to set
     * roDeleted to true in the trashed object.
     * 
     * @param undoHash
     *            (Optional) When null, RedoraTrash will calculate a new hash.
     * @param objectId
     *            (Mandatory) Id of the trashed object
     * @param objectName
     *            (Mandatory) Name of the object
     * @throws PersistException
     *             When a business rule is violated an exception is thrown
     *             because this should be unexpected.
     */
    public static void trash(String undoHash, Long objectId, String objectName)
            throws ConnectException, PersistException, BusinessRuleViolationException,
            QueryException {
        RedoraTrash trash = new RedoraTrash();
        if (undoHash != null) {
            trash.setUndoHash(undoHash);
        }
        trash.setObjectId(objectId);
        trash.setObjectName(objectName);
        Set<BusinessRuleViolation> ret = ServiceFactory.redoraTrashService().persist(trash);
        if (!ret.isEmpty()) {
            throw new PersistException("Unexpected business rule violation(s). " + ret.size()
                    + " rules have been violated, the first rule is "
                    + ret.iterator().next().getBusinessRuleId());
        }
    }

    /**
     * Deletes the RedoraTrash. It will NOT delete the associated trashed
     * records.
     * 
     * @param objectId
     *            (Mandatory)
     * @param objectName
     *            (Mandatory)
     * @throws PersistException
     *             When a business rule is violated an exception is thrown
     *             because this should be unexpected.
     */
    public static void deleteTrash(Long objectId, String objectName) throws ConnectException,
            PersistException, BusinessRuleViolationException, QueryException, CopyException, PagingException {
        List<Object> params = new Vector<Object>();
        params.add(objectId);
        params.add(objectName);
        List<RedoraTrash> trashList = ServiceFactory.redoraTrashService().find(
                RedoraTrashSQL.FIND_BY_NAME_AND_ID, params, Page.ALL_LIST);
        if (trashList.size() != 1) {
            throw new QueryException("I expected to find one RedOrangeTrash, but i found: "
                    + trashList.size());
        }

        Set<BusinessRuleViolation> ret = ServiceFactory.redoraTrashService().delete(
                trashList.iterator().next());
        if (!ret.isEmpty()) {
            throw new PersistException("Unexpected business rule violation(s). " + ret.size()
                    + " rules have been violated, the first rule is "
                    + ret.iterator().next().getBusinessRuleId());
        }
    }
}
